// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'cloud1-2ga6780q302b7f4a'
}) // 使用当前云环境
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openId = wxContext.OPENID
  const { status = 'all' } = event // 订单状态，可选值：ongoing(进行中), history(历史订单), all(全部)
  
  try {
    // 构建查询条件
    const query = { openId }
    
    // 根据状态筛选
    if (status === 'ongoing') {
      query.status = '借用中'
    } else if (status === 'history') {
      query.status = '已归还'
    }
    
    // 执行查询
    const { data } = await db.collection('orders')
      .where(query)
      .orderBy('createTime', 'desc')
      .get()
    
    return {
      code: 0,
      data,
      message: '获取订单列表成功'
    }
  } catch (err) {
    return {
      code: -1,
      message: '获取订单列表失败: ' + err.message
    }
  }
} 